package controllers

import (
	beego "github.com/beego/beego/v2/server/web"
	"registration-display/models"
)

type OrderController struct {
	beego.Controller
}

func (c *OrderController) Get() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Methods", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Headers", "*")
	if c.Ctx.Input.Method() == "OPTIONS" {
		c.Ctx.Output.SetStatus(200)
		c.StopRun()
	}
	id, _ := c.GetInt("id")
	var data models.OrderData
	err := models.DB.Model(&models.Order{}).Joins("join users on orders.uid = users.id join hospitals on orders.h_id = hospitals.id join doctors on orders.did = doctors.id").Select("orders.*,users.name,users.card_type,users.card_number,users.tel,users.user_name,users.address,users.image,users.relationship,hospitals.hos_address,hospitals.hos_name,doctors.doc_name,doctors.price,doctors.dep_name,doctors.pro_title").Where("users.id = ?", id).Limit(1).Scan(&data).Error
	if err != nil {
		c.Data["json"] = map[string]any{
			"Code": 501,
			"Msg":  "服务器繁忙",
		}
		c.ServeJSON()
		c.StopRun()
	}
	if data.Image == "" {
		data.Image = "http://127.0.0.1:8080/static/img/屏幕截图 2024-08-22 110505.png"
	}
	data.Address = data.Address[:15] + "*****"
	data.HosAddress = data.HosAddress[:15] + "*****"
	c.Data["json"] = map[string]any{
		"Code": 200,
		"Msg":  "获取成功",
		"Data": data,
	}
	c.ServeJSON()
	c.StopRun()
}
